package aop;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: omybug
 * @date: 18/9/29 11:20
 */
public class CglibObjectProxy {

    private Object target;

    public Object getProxy(Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
//        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("** 开始 **");
        System.out.println("method name : " + method.getName());
        Object result = proxy.invokeSuper(obj, args);
        System.out.println("** 结束 **");
        return result;
    }
}
